package xjh.him.common;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;


public abstract class MyBaseAdapter<T> extends BaseAdapter {
    private List<T> beans;
    private int layoutRes;

    public MyBaseAdapter(List<T> beans, int layoutRes) {
        this.beans = beans;
        this.layoutRes = layoutRes;
    }

    @Override
    public int getCount() {
        return beans != null ? beans.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return beans != null ? beans.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup parent) {
        MyViewHolder holder = MyViewHolder.bind(i, view, parent, layoutRes);
        bindView(holder, beans.get(i));
        return holder.getItemView();
    }

    public abstract void bindView(MyViewHolder holder, T bean);

    public void addLast(T bean) {
        add(beans.size(), bean);
    }

    public void add(int idx, T bean) {
        if (beans == null) {
            beans = new ArrayList<>();
        }
        beans.add(idx, bean);
        notifyDataSetChanged();
    }

    public void remove(int pos) {
        if (beans != null) {
            beans.remove(pos);
        }
        notifyDataSetChanged();
    }
}
